<?php
/**
 * 卡片统计
 * User: Acer
 * Date: 2022/7/23
 * Time: 13:35
 */
namespace app\crontab\service;

use app\card\models\CardModel;
use app\card\models\CardProModel;
use app\card\models\CardTypeModel;
use app\card\models\UserCardModel;
use app\crontab\models\CardCensusModels;

class  CardShellService{
    // 总激活卡片数
    public function s_card_active_total($condition = []){
        $UserCardModel = new UserCardModel();
        if($condition){
            $UserCardModel->where($condition);
        }
        $cu_id_number = $UserCardModel->field("cu_id as card_number")->count("cu_id");
        return $cu_id_number;
    }

    // 卡片分类统计
    public function s_active_card_total($condition = []){
        $UserCardModel = new UserCardModel();
        if($condition){
            $UserCardModel->where($condition);
        }
        $cardIdData = $UserCardModel->field("pid")->select();
        $cardIdData = obj_to_arr($cardIdData);
        if(!$cardIdData) return 0;
        $tempArr = array_column($cardIdData, 'pid');
        $cardTypeNumber = [];
        $cardTypeNumber['family_number'] = 0;  // 家庭卡
        $cardTypeNumber['personal_number'] = 0;  // 个人卡
        $cardTypeNumber['my_number'] = 0;  // 名医卡
        $cardTypeNumber['qmx_number'] = 0;  // 全明星卡
        // 卡片产品
        $CardProModel = new CardProModel();
        // 卡片
        $CardModel = new CardModel();
        // 卡片类型
        $CardTypeModel = new CardTypeModel();
        foreach($tempArr as $key=>$value){
            // 获取卡片id
            $cid = $CardProModel->where('pid',$value)->value('cid');
            if(!$cid) return false;
            // 根据卡片id，获取卡片类型
            $ct_id = $CardModel->where('cid',$cid)->value('ct_id');
            if(!$ct_id) return false;
            $cardTypeInfo = $CardTypeModel->where('id',$ct_id)->find();
            $cardTypeInfo = obj_to_arr($cardTypeInfo);
            if(!$cardTypeInfo) return false;
            // 卡片类型（1：个人 、2：家庭、3：全明星、4：名医）
            if((int)$cardTypeInfo['type'] == 1){
                $cardTypeNumber['personal_number'] = $cardTypeNumber['personal_number'] + 1;
            }else if((int)$cardTypeInfo['type'] == 2){
                $cardTypeNumber['family_number'] = $cardTypeNumber['family_number'] + 1;
            }else if((int)$cardTypeInfo['type'] == 3){
                $cardTypeNumber['qmx_number'] = $cardTypeNumber['qmx_number'] + 1;
            }else if((int)$cardTypeInfo['type'] == 4){
                $cardTypeNumber['my_number'] = $cardTypeNumber['my_number'] + 1;
            }
        }
        return $cardTypeNumber;
    }

    public function s_save_census($data){
        $CardCensusModels = new CardCensusModels();
        $sc_id = $CardCensusModels->where('census_time',$data['census_time']." 00:00:00")->value('sc_id');
        $install =[
            'total_number'  =>  (int)$data['total_number'], // 总计卡片数
            'active_number'  =>  (int)$data['active_number'], // 激活卡片总数
            'active_family_number'  =>  (int)$data['active_family_number'], // 家庭卡激活数
            'active_personal_number'  =>  (int)$data['active_personal_number'], // 个人卡激活数
            'active_qmx_number'  =>  (int)$data['active_qmx_number'], // 全明星卡激活数
            'active_my_number'  =>  (int)$data['active_my_number'], // 名医卡激活数
            'add_number'  =>  (int)$data['add_number'], // 今日新增卡片数
            'day_active_number'  =>  (int)$data['day_active_number'], // 今日片数总激活数
            'day_active_family_number'  =>  (int)$data['day_active_family_number'], // 今日新增激活家庭卡片数
            'day_active_personal_number'  =>  (int)$data['day_active_personal_number'], // 今日新增激活个人卡片数
            'day_active_qmx_number'  =>  (int)$data['day_active_qmx_number'], // 今日新增激活qmx卡片数
            'day_active_my_number'  =>  (int)$data['day_active_my_number'], // 今日新增激活my卡片数
            'census_time'  =>  $data['census_time'], // 统计时间
        ];
        if($sc_id){
            // 更新
            $CardCensusModels->save($install,[
                'sc_id' => $sc_id
            ]);
        }else{
            // 新增
            $CardCensusModels->insertGetId($install);
        }
    }
    // 获取卡片数
    public function s_card_add_total($condition = []){
        $CardProModel = new CardProModel();
        if($condition){
            $CardProModel->where($condition);
        }
        $card_number = $CardProModel->field("pid as card_number")->count("pid");
        return $card_number;
    }


}